import telebot
import requests

bot = telebot.TeleBot('6375307890:AAHNgqhOJfGYn-Ya4nsMfINvkbFpCPhE6QA')

@bot.message_handler(commands=['start'])
def start(message):
	data = bot.send_message(message.chat.id, 'Привет! Отправь твой токен из дневника, чтобы получить доступ к боту!')
	bot.register_next_step_handler(data, start1)
def start1(message):
	file = open('general.txt', 'a+')
	file.write(str(message.from_user.username) + '@' + message.text + '\n')
	bot.send_message(message.chat.id, 'Готово, ты залогинился в бота! Выбери, что ты хочешь сделать:\nДомашки - /homeworks\nРасписание - /schedule\nОценки за день - /marks')

@bot.message_handler(commands=['schedule'])
def schedule(message):
	date = bot.send_message(message.chat.id, 'Напиши дату, на которую надо посмотреть расписание!')
	bot.register_next_step_handler(date, schedule1)
def schedule1(message):
	date = message.text
	file = open('general.txt', 'r')
	line = file.read()
	array = line.split('\n')
	for i in array:
		if message.from_user.username in i:
			i_split = i.split('@')
			tgname = i_split[0]
			token = i_split[1]
			user_data = requests.get(
					"https://dnevnik.mos.ru/core/api/student_profiles/",
					headers={
						"Auth-Token": token
					}
				)
			idd = user_data.json()[0]["id"]
			res = []
			response = requests.get(
				f"https://school.mos.ru/api/family/web/v1/schedule?student_id={idd}&date={date}",
				headers={
					"Auth-Token": token,
					"X-Mes-Subsystem": "familyweb"
				}
			)
			for schedule in response.json()["activities"]:
				if 'lesson' in schedule and 'subject_name' in schedule['lesson'] and 'begin_time' in schedule and 'end_time' in schedule:
					res.append(schedule['lesson']['subject_name'] + ': ' + schedule['begin_time'] + '-' + schedule['end_time'])
			gen = ''
			for i in res:
				gen = gen + i + '\n'
			print(gen)
			bot.send_message(message.chat.id, 'Твоё расписание на ' + date + ':\n\n' + str(gen))
		
@bot.message_handler(commands=['homeworks'])
def homeworks(message):
	date = bot.send_message(message.chat.id, 'Напиши дату, на которую надо посмотреть домашки!')
	bot.register_next_step_handler(date, homeworks1)
def homeworks1(message):
	date = message.text
	file = open('general.txt', 'r')
	line = file.read()
	array = line.split('\n')
	for i in array:
		if message.from_user.username in i:
			i_split = i.split('@')
			tgname = i_split[0]
			token = i_split[1]
			user_data = requests.get(
					"https://dnevnik.mos.ru/core/api/student_profiles/",
					headers={
						"Auth-Token": token
					}
				)
			idd = user_data.json()[0]["id"]
			res = []
			response = requests.get(
				f"https://school.mos.ru/api/family/web/v1/homeworks?from={date}&to={date}&student_id={idd}",
				headers={
					"Auth-Token": token,
					"X-Mes-Subsystem": "familyweb"
				}
			)
			for homework in response.json()["payload"]:
				if 'subject_name' in homework and 'description' in homework:
					res.append(homework['subject_name'] + ': ' + homework['description'])
			gen = ''
			for i in res:
				gen = gen + i + '\n'
			bot.send_message(message.chat.id, 'Твои домашки на ' + date + ':\n\n' + str(gen))

@bot.message_handler(commands=['marks'])
def marks(message):
	date = bot.send_message(message.chat.id, 'Напиши дату, на которую надо посмотреть оценки!')
	bot.register_next_step_handler(date, marks1)
def marks1(message):
	date = message.text
	file = open('general.txt', 'r')
	line = file.read()
	array = line.split('\n')
	for i in array:
		if message.from_user.username in i:
			i_split = i.split('@')
			tgname = i_split[0]
			token = i_split[1]
			user_data = requests.get(
            	"https://dnevnik.mos.ru/core/api/student_profiles/",
				headers={
					"Auth-Token": token
				}
			)
			idd = user_data.json()[0]["id"]
			res = []
			response = requests.get(
				f"https://school.mos.ru/api/family/web/v1/schedule?student_id={idd}&date={date}",
				headers={
					"Auth-Token": token,
					"X-Mes-Subsystem": "familyweb"
				}
			)
			for activity in response.json()["activities"]:
				if 'lesson' in activity and 'marks' in activity['lesson'] and 'subject_name' in activity['lesson']:
					for mark in activity['lesson']['marks']:
						value = mark['value']
						res.append(activity['lesson']['subject_name'] + ': ' + value)
			gen = ''
			for i in res:
				gen = gen + i + '\n'
			bot.send_message(message.chat.id, 'Твои оценки за ' + date + ':\n\n' + str(gen))
bot.polling(interval = 0, none_stop = True)